<template>
  <el-select
    class="input"
    v-model="inputVal"
    clearable
    @clear="change(null)"
    :disabled="isReadonly"
    :style="{ width: config.width ? config.width : '215px ' }"
    :multiple="config.multiple"
    @change="change"
    :placeholder="isReadonly ? '' : `请选择${config.name}`"
  >
    <el-option
      v-for="item in config.options"
      :key="config.config ? item[config.config.value] : item.value"
      :label="config.config ? item[config.config.label] : item.label"
      :value="config.config ? item[config.config.value] : item.value"
      :disabled="item.disabled"
    >
    </el-option>
  </el-select>
</template>
<script>
import Inject from "../../../mixin/inject";
export default {
  mixins: [Inject],
  props: {
    isReadonly: {
      type: Boolean,
      default: false,
    },
    formData: Object,
    config: {
      type: Object,
      default: () => Object.create(null),
    },
  },
  data() {
    return {
      inputVal: null,
    };
  },
  computed: {
    curVal() {
      return this.formData[this.config.prop] || "";
    },
  },
  watch: {
    curVal: {
      immediate: true,
      deep: true,
      handler(val) {
        this.inputVal = val;
      },
    },
  },
  created() {},
  methods: {
    change(val) {
      this.inputVal = val;
      if (this.config.multiple) {
        this.formData[this.config.prop] = val;
      }

      // 自定义change事件
      if (
        this.config.change &&
        this.$CD[this.config.change] instanceof Function
      ) {
        this.$CD[this.config.change](val, this.config);
      }
      this.formData[this.config.prop] = val;
      this.$emit("update:formData", this.formData);
      this.$emit("change");
    },
  },
};
</script>
<style lang="scss" scoped></style>
